package com.homelock.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.homelock.domain.LockTemporaryPwdPostForm;
import com.homelock.service.LockTemporaryPwdService;
import com.pi.base.dto.result.AppResult;
import com.pi.http.base.controller.BaseController;

@RequestMapping("/lock/opmode/temporarypwd")
@RestController
public class LockTemporaryPwdController extends BaseController{
  
  private LockTemporaryPwdService lockTemporaryPwdService;

  @Autowired
  public LockTemporaryPwdController(LockTemporaryPwdService lockTemporaryPwdService){
    Assert.notNull(lockTemporaryPwdService, "锁具临时密码服务为空");
    this.lockTemporaryPwdService = lockTemporaryPwdService;
  }
  
  @RequestMapping("/generate")
  public AppResult generateTemporaryPwd(Long devId){
    return AppResult.newSuccessResult(
        lockTemporaryPwdService.generateTemporaryPwd(devId, getLoginUserId()));
  }
  
  @RequestMapping("/confirm")
  public AppResult confirmRecordPassword(@RequestBody List<LockTemporaryPwdPostForm> passwords){
    lockTemporaryPwdService.confirmRecordPassword(passwords);
    return AppResult.OK;
  }
  
  @RequestMapping("/otp")
  public AppResult getOneTimePassword(Long devId) {
    return AppResult.newSuccessResult(lockTemporaryPwdService.getOneTimePassword(devId));
  }
  
  @RequestMapping("/otp/v2")
  public AppResult getOneTimePasswordByAdminPwd(Long devId, Integer adminPwd) {
    return AppResult.newSuccessResult(lockTemporaryPwdService.getOneTimePasswordByAdmin(devId, adminPwd));
  }
}
